package com.lai.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.hospital.dao.DepartmentDao;
import com.lai.hospital.dto.DepartmentDTO;
import com.lai.hospital.entity.Department;
import com.lai.hospital.exception.BizException;
import com.lai.hospital.service.DepartmentService;
import com.lai.hospital.utils.BeanCopyUtils;
import com.lai.hospital.vo.ConditionVO;
import com.lai.hospital.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.service.impl
 * @Description: 科室服务
 * @date Date : 2022年11月24日 1:08
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, Department> implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;



    @Override
    public List<DepartmentDTO> listDepartment(ConditionVO conditionVO) {
        //1、根据Keywords获取全部科室列表
        List<Department> departmentList = departmentDao.selectList(new LambdaQueryWrapper<Department>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Department::getDepartmentName, conditionVO.getKeywords()));
        //2、获取科室主目录列表
        List<Department> departmentCatalog = listDepartmentCatalog(departmentList);
        //3、获取子科室
        Map<Integer, List<Department>> childrenDepartmentMap = getChildrenDepartment(departmentList);
        //4、组装科室目录数据
        List<DepartmentDTO> departmentDTOList = departmentCatalog.stream().map(item -> {
            //父科室
            DepartmentDTO departmentDTO = BeanCopyUtils.copyObject(item, DepartmentDTO.class);
            //父科室设置级联值
            departmentDTO.setValue(departmentDTO.getValue());
            departmentDTO.setLabel(departmentDTO.getDepartmentName());
            departmentDTO.setText(departmentDTO.getDepartmentName());
            //根据父科室id获取子科室
            List<DepartmentDTO> childrenListDepartmentDTO = BeanCopyUtils.copyList(childrenDepartmentMap.get(item.getId()), DepartmentDTO.class);
            //子科室设置级联值
            List<DepartmentDTO> childrenListDepartment = childrenListDepartmentDTO.stream().map(departmentDTOItem -> {
                departmentDTOItem.setLabel(departmentDTOItem.getDepartmentName());
                departmentDTOItem.setText(departmentDTOItem.getDepartmentName());
                departmentDTOItem.setValue(departmentDTOItem.getId());
                return departmentDTOItem;
            }).collect(Collectors.toList());
            //设置子科室
            departmentDTO.setChildren(childrenListDepartment);
            childrenDepartmentMap.remove(item.getId());
            return departmentDTO;
        }).collect(Collectors.toList());
        // 若还有菜单未取出则拼接
        if (CollectionUtils.isNotEmpty(childrenDepartmentMap)) {
            List<Department> childrenList = new ArrayList<>();
            childrenDepartmentMap.values().forEach(childrenList::addAll);
            List<DepartmentDTO> childrenDTOList = childrenList.stream()
                    .map(item -> BeanCopyUtils.copyObject(item, DepartmentDTO.class))
                    .collect(Collectors.toList());
            departmentDTOList.addAll(childrenDTOList);
        }

        return departmentDTOList;
    }

    @Override
    public void saveOrUpdateDepartment(DepartmentVO departmentVO) {
        Department department = BeanCopyUtils.copyObject(departmentVO, Department.class);
        this.saveOrUpdate(department);
    }


    @Override
    public void deleteDepartment(Integer departmentId) {
        Integer count = departmentDao.selectCount(new LambdaQueryWrapper<Department>().eq(Department::getParentId, departmentId));
        if (count > 0) {
            //TODO: 还有医生关联
            throw new BizException("此科室下有子科室关联，请先删除子科室");
        }
        departmentDao.deleteById(departmentId);
    }

    /**
     * 医师使用到的科室
     *
     * @return
     */
    @Override
    public List<DepartmentDTO> listDepartmentChildren() {
        List<Department> departmentList = departmentDao.selectList(new LambdaQueryWrapper<Department>().isNotNull(Department::getParentId));
        return BeanCopyUtils.copyList(departmentList, DepartmentDTO.class);
    }


    /**
     * 获取主科室
     *
     * @return 科室主目录
     */
    public List<Department> listDepartmentCatalog(List<Department> departmentList) {
        return departmentList.stream()
                //获取到parentId为null的数据(说明是第一级科室目录)
                .filter(item -> Objects.isNull(item.getParentId()))
                .collect(Collectors.toList());
    }

    /**
     * 获取子科室
     *
     * @param departmentList 全部科室
     * @return 子科室
     */
    public Map<Integer, List<Department>> getChildrenDepartment(List<Department> departmentList) {
//        List<Department> departments = departmentList.stream()
//                //获取到parentId不为null的数据(说明有父目录)
//                .filter(item -> Objects.nonNull(item.getParentId()))
//                .collect(Collectors.toList());
//        List<DepartmentDTO> departmentDTOList = BeanCopyUtils.copyList(departments, DepartmentDTO.class);
//        R collect = departmentDTOList.stream().map(item -> {
//            item.setValue(item.getId());
//            item.setLabel(item.getName());
//            return item;
//        }).collect(Collectors.groupingBy(Department::getParentId));
        return departmentList.stream()
                //获取到parentId不为null的数据(说明有父目录)
                .filter(item -> Objects.nonNull(item.getParentId()))
                .collect(Collectors.groupingBy(Department::getParentId));
    }


}
